package com.gaoyi.crm.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultLauncher;
import androidx.core.content.FileProvider;
import androidx.fragment.app.FragmentActivity;

import com.gaoyi.crm.R;

import java.io.File;
import java.io.IOException;
import java.util.Date;

public class PhotoUtils {

    private static volatile PhotoUtils instance;
    public int REQUEST_CAMERA = 1;
    public int REQUEST_GALLERY = 2;

    public static PhotoUtils getInstance() {
        if (instance == null) {
            synchronized (PhotoUtils.class) {
                if (instance == null) {
                    instance = new PhotoUtils();
                }
            }
        }
        return instance;
    }

    private String mImgUrl;

    public void toCamera(Activity activity, ActivityResultLauncher<Intent> resultLauncher) {
        PackageManager pm = activity.getPackageManager();
        if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)) {
            ToastUtil.showToast(R.string.no_camera);
            return;
        }
        try {
            String imgName = getImgName();
            File photoFile = new File(activity.getExternalCacheDir(), imgName);
            File parentFile = photoFile.getParentFile();
            if (!parentFile.exists()) parentFile.mkdirs();
            if (photoFile.exists()) photoFile.delete();
            photoFile.createNewFile();
            mImgUrl = photoFile.getAbsolutePath();
            Uri imgUri = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ?
                    FileProvider.getUriForFile(activity, activity.getPackageName() + ".provider", photoFile)
                    : Uri.fromFile(photoFile);
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
            resultLauncher.launch(intent);
        } catch (Exception e) {
            e.printStackTrace();
            ToastUtil.showToast(R.string.no_camera);
        }
    }

    public String getImgName() {
        return "img_" + DateUtil.DateTimeToStr(new Date(), "yyyyMMddHHmmss") + ".jpg";
    }

    public String getCameraResult(FragmentActivity activity) {
        Bitmap bitmap = BitmapUtils.getInstance().resizeBitmap(mImgUrl);
        int angle = getCameraAngle(mImgUrl);
        if (angle != 0) {
            bitmap = BitmapUtils.getInstance().rotatingBitmap(bitmap, angle);
        }
        return BitmapUtils.getInstance().bitmap2Url(activity, bitmap, mImgUrl);
    }

    /**
     * 获取图片旋转角度
     */
    private int getCameraAngle(String imgUrl) {
        int angle = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(imgUrl);
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    angle = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    angle = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    angle = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return angle;
    }

    public void toFile(ActivityResultLauncher<Intent> resultLauncher) {
        try {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("*/*");
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            resultLauncher.launch(intent);
        } catch (Exception e) {
            ToastUtil.showToast(R.string.no_file);
        }
    }

    public String getFileResult(Context context, ActivityResult result) {
        Intent data = result.getData();
        if (data == null) return null;
        return FileUtils.getInstance().getFilePathByUri(context, data.getData());
    }

}
